package chen.web.stat;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class StatSevlet
 */
public class StatServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final String Client_Screen_Width = "screenWidth";
	private static final String Client_Screen_Height = "screenHeight";
	private static final String Client_Browser_Width = "clientWidth";
	private static final String Client_Browser_Height = "clientHeight";
	private static final String Client_Time_Zone = "timeZone";
	private static final String Client_Color = "color";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StatServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int statId = toInt(request.getParameter("id"));
		if(statId == 0){
			return;
		}
		int screenWidth = toInt(request.getParameter(Client_Screen_Width));
		int screenHeight = toInt(request.getParameter(Client_Screen_Height));
		int clientWidth = toInt(request.getParameter(Client_Browser_Width));
		int clientHeight = toInt(request.getParameter(Client_Browser_Height));
		int clienTimeZone = toInt(request.getParameter(Client_Time_Zone));
		int clientColor = toInt(request.getParameter(Client_Color));
		
		ClientInfo info = new ClientInfo();
		info.setStatId(statId);
		info.setScreenColor(clientColor);
		info.setClientHeight(clientHeight);
		info.setClienTimeZone(clienTimeZone);
		info.setClientWidth(clientWidth);
		info.setScreenHeight(screenHeight);
		info.setScreenWidth(screenWidth);
		Statistics.update(info);
	}

	private int toInt(String numeric){
		int result = 0;
		try{
			result = Integer.parseInt(numeric);	
		} catch(RuntimeException ex){
			
		}
		return result;
	}
}
